using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using admin.application.Services;
using admin.application.Dtos;
using admin.core.Common;
using admin.application.Common;
using admin.core.Entities.App;
using Microsoft.AspNetCore.Authorization;

namespace admin.api.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class UserController : ControllerBase
    {
        private readonly IAppUserService _userService;
        public UserController(IAppUserService userService)
        {
            _userService = userService;
        }

        [HttpGet]
        public async Task<IActionResult> GetAll([FromQuery] PaginationRequest request)
        {
            var result = await _userService.GetPagedAsync(request);
            return Ok(ResponseResult<PaginationResult<UserDto>>.Ok(result, "获取用户列表成功"));
        }

        [HttpGet("all")]
        public async Task<IActionResult> GetAllUsers()
        {
            var users = await _userService.GetAllAsync();
            return Ok(ResponseResult<IEnumerable<UserDto>>.Ok(users, "获取用户列表成功"));
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetById(Guid id)
        {
            var user = await _userService.GetByIdAsync(id);
            return Ok(ResponseResult<UserDto>.Ok(user, "获取用户信息成功"));
        }

        [HttpPost]
        public async Task<IActionResult> Create([FromBody] CreateUserDto dto)
        {
            await _userService.CreateAsync(dto);
            return Ok(ResponseResult<object>.Ok(null, "创建用户成功"));
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserDto dto)
        {
            await _userService.UpdateAsync(id, dto);
            return Ok(ResponseResult<object>.Ok(null, "更新用户信息成功"));
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            try
            {
                await _userService.DeleteAsync(id);
                return Ok(ResponseResult<object>.Ok(null, "删除用户成功"));
            }
            catch (NotFoundException ex)
            {
                return NotFound(ResponseResult<object>.Fail(ex.Message, 404));
            }
        }

        [HttpPost("{id}/enable")]
        public async Task<IActionResult> Enable(Guid id)
        {
            try
            {
                await _userService.EnableAsync(id);
                return Ok(ResponseResult<object>.Ok(null, "启用用户成功"));
            }
            catch (NotFoundException ex)
            {
                return NotFound(ResponseResult<object>.Fail(ex.Message, 404));
            }
        }

        [HttpPost("{id}/disable")]
        public async Task<IActionResult> Disable(Guid id)
        {
            try
            {
                await _userService.DisableAsync(id);
                return Ok(ResponseResult<object>.Ok(null, "禁用用户成功"));
            }
            catch (NotFoundException ex)
            {
                return NotFound(ResponseResult<object>.Fail(ex.Message, 404));
            }
        }

        [HttpPost("{id}/assign-role")]
        public async Task<IActionResult> AssignRole(Guid id, [FromQuery] Guid roleId)
        {
            await _userService.AssignRoleAsync(id, roleId);
            return Ok(ResponseResult<object>.Ok(null, "分配角色成功"));
        }

        [HttpPost("{id}/remove-role")]
        public async Task<IActionResult> RemoveRole(Guid id, [FromQuery] Guid roleId)
        {
            await _userService.RemoveRoleAsync(id, roleId);
            return Ok(ResponseResult<object>.Ok(null, "移除角色成功"));
        }

        [HttpPost("{id}/change-password")]
        public async Task<IActionResult> ChangePassword(Guid id, [FromBody] ChangePasswordDto dto)
        {
            try
            {
                await _userService.ChangePasswordAsync(id, dto.OldPassword, dto.NewPassword);
                return Ok(ResponseResult<object>.Ok(null, "修改密码成功"));
            }
            catch (NotFoundException ex)
            {
                return Ok(ResponseResult<object>.Fail(ex.Message, 404));
            }
            catch (Exception ex)
            {
                return Ok(ResponseResult<object>.Fail(ex.Message, 400));
            }
        }

        [HttpGet("{id}/roles")]
        public async Task<IActionResult> GetUserRoles(Guid id)
        {
            try
            {
                var roles = await _userService.GetUserRolesAsync(id);
                return Ok(ResponseResult<List<AppRole>>.Ok(roles, "获取用户角色成功"));
            }
            catch (NotFoundException ex)
            {
                return NotFound(ResponseResult<object>.Fail(ex.Message, 404));
            }
        }

        [HttpGet("{id}/contact-info")]
        public async Task<IActionResult> GetContactInfo(Guid id)
        {
            var info = await _userService.GetContactInfoAsync(id);
            return Ok(ResponseResult<ContactInfoDto>.Ok(info, "获取联系方式成功"));
        }

        [HttpPost("contact-info")]
        public async Task<IActionResult> UpdateContactInfo([FromBody] UpdateContactInfoDto dto)
        {
            await _userService.UpdateContactInfoAsync(dto.UserId, dto.Phone, dto.Email);
            return Ok(ResponseResult<object>.Ok(null, "联系方式更新成功"));
        }
    }
}
